package campus.aiit.ssl.manage.modbus;

import campus.aiit.ssl.manage.modbus.provider.Provider;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * Command Wrapper
 *
 * @author ihaokun
 * @date 2019/12/17 22:19
 */
@Component
public class ModBusCommand {
  @Resource
  private SocketForModBus socketForModBus;

  /* 自控：状态 + 开 + 关 */
  public boolean autoStatus(SocketForModBus.SDU sdu){
    buildSDU(sdu, Provider.Constants.PDUs.AUTO_STATUS);
    List<Integer> list = socketForModBus.perform(sdu,"自控：状态 + 开 + 关");
    return Parse.ofReadStatus(list);
  }
  /**
   * zigbeeStatus
   * @return
   */
  public Integer zigbeeStatus(SocketForModBus.SDU sdu) {
    buildSDU(sdu, Provider.Constants.PDUs.ZIGBEE_STATUS);
    List<Integer> list = socketForModBus.perform(sdu,"zigbeeStatus");
    return Parse.ofzigbeeStatus(list);
  }
  public Integer zigbeeStatusFs(SocketForModBus.SDU sdu) {
    buildSDU(sdu, Provider.Constants.PDUs.ZIGBEE_STATUS_FS);
    List<Integer> list = socketForModBus.perform(sdu,"zigbeeStatusFs");
    return Parse.ofzigbeeStatusFs(list);
  }
  public boolean turnOnAuto(SocketForModBus.SDU sdu){
    buildSDU(sdu, Provider.Constants.PDUs.ON_AUTO);
    List<Integer> list = socketForModBus.perform(sdu,"turnOnAuto");
    return Parse.ofWrite(list);
  }
  public boolean turnOffAuto(SocketForModBus.SDU sdu){
    buildSDU(sdu, Provider.Constants.PDUs.OFF_AUTO);
    List<Integer> list = socketForModBus.perform(sdu,"turnOffAuto");
    return Parse.ofWrite(list);
  }
  /* 继电器：状态 + 开 + 关 */
  public boolean relayStatus(SocketForModBus.SDU sdu){
    buildSDU(sdu, Provider.Constants.PDUs.RELAY_STATUS);
    List<Integer> list = socketForModBus.perform(sdu,"继电器：状态 + 开 + 关");
    return Parse.ofReadStatus(list);
  }
  public boolean turnOnRelay(SocketForModBus.SDU sdu){
    buildSDU(sdu, Provider.Constants.PDUs.ON_RELAY);
    List<Integer> list = socketForModBus.perform(sdu,"turnOnRelay");
    return Parse.ofWrite(list);
  }
  public boolean turnOffRelay(SocketForModBus.SDU sdu){
    buildSDU(sdu, Provider.Constants.PDUs.OFF_RELAY);
    List<Integer> list = socketForModBus.perform(sdu,"turnOffRelay");
    return Parse.ofWrite(list);
  }
  /* 开关状态 */
  public boolean onOffStatus(SocketForModBus.SDU sdu){
    buildSDU(sdu, Provider.Constants.PDUs.ON_OFF_STATUS);
    List<Integer> list = socketForModBus.perform(sdu,"开关状态");
    return Parse.ofReadStatus(list);
  }
  /* 蜂鸣器状态 */
  public boolean buzzerStatus(SocketForModBus.SDU sdu) {
    buildSDU(sdu, Provider.Constants.PDUs.BUZZER_STATUS);
    List<Integer> list = socketForModBus.perform(sdu,"蜂鸣器状态");
    return Parse.ofReadStatus(list);
  }
  /* 冬夏季模式 */
  public boolean mode(SocketForModBus.SDU sdu) {
    buildSDU(sdu, Provider.Constants.PDUs.MODE);
    List<Integer> list = socketForModBus.perform(sdu,"冬夏季模式");
    return Parse.ofReadStatus(list);
  }
  /* 温度℃ */
  public int temperature(SocketForModBus.SDU sdu){
    buildSDU(sdu, Provider.Constants.PDUs.TEMPERATURE);
    List<Integer> list = socketForModBus.perform(sdu,"温度");
    return Parse.ofReadTemperature(list);
  }
  /* 用电量kw.h */
  public int electricConsume(SocketForModBus.SDU sdu){
    buildSDU(sdu, Provider.Constants.PDUs.ELECTRIC_CONSUME);
    List<Integer> list = socketForModBus.perform(sdu,"用电量kw");
    return Parse.ofReadElectricConsume(list);
  }
  /* 电压V */
  public int volts(SocketForModBus.SDU sdu){
    buildSDU(sdu, Provider.Constants.PDUs.VOLTS);
    List<Integer> list = socketForModBus.perform(sdu,"电压V");
    return Parse.ofReadVolts(list);
  }
  /* 电流mA */
  public int amperes(SocketForModBus.SDU sdu) {
    buildSDU(sdu, Provider.Constants.PDUs.VOLTS);
    List<Integer> list = socketForModBus.perform(sdu,"电流mA");
    return Parse.ofReadAmperes(list);
  }

  /** Filling SDU by PDUs, PDUs(String) to PDU(Object) */
  private void buildSDU(SocketForModBus.SDU sdu, Provider.Constants.PDUs providePdu){
    ModBus.ADU.PDU pdu = new ModBus.ADU.PDU(providePdu.toString());
    sdu.getAdu().setPdu(pdu);
  }

  // 0x10 Write Multiple registers
  public boolean infrared(String host, int port, String addressCode, String value) {
    SocketForModBus.SDU sdu = new SocketForModBus.SDU(host, port);
    // combine SDU(host, port, ADU)
    ModBus.ADU adu = new ModBus.ADU();
    adu.setAddress(addressCode);
    ModBus.ADU.PDU pdu = new ModBus.ADU.PDU(value);
    adu.setPdu(pdu);
    sdu.setAdu(adu);
    List<Integer> list = socketForModBus.perform(sdu,"infrared");
    return Parse.ofWrite(list);
  }
  public boolean writeStrategy(SocketForModBus.SDU sdu){
    List<Integer> list = socketForModBus.perform(sdu,"writeStrategy");
    return Parse.ofWriteMultipleRegisters(list);
  }



}